//
//  DateFormat+Ext.swift
//  TimMusic
//
//  Created by nkl on 2024/10/25.
//

import Foundation

extension Locale {
    static let isLanguageZh: Bool = {
        if appLanguage.lowercased().hasPrefix("zh") {
            return true
        }
        return false
    }()
}

extension DateFormatter {
    static var Formatters = [String: DateFormatter]()

    static let english: DateFormatter = {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: "en_US")
        return dateFormatter
    }()

    static func formatter(with format: String) -> DateFormatter {
        if let dateFormatter = DateFormatter.Formatters[format] {
            return dateFormatter
        }
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        dateFormatter.locale = Locale.local
        DateFormatter.Formatters[format] = dateFormatter
        return dateFormatter
    }
}

extension DateFormatter {
    // 仅用此格式化存储，禁止修改！！！
    static let full: DateFormatter = {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "en")
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return formatter
    }()

    func fullDateString(_ date: Date = Date()) -> String {
        return DateFormatter.formatter(with: "yyyy-MM-dd").string(from: date)
    }

    var dotDateString: String {
        return DateFormatter.formatter(with: Locale.isLanguageZh ? "yyyy.MM.dd" : "MM.dd.yyyy").string(from: Date())
    }

    var yeer: String {
        return DateFormatter.formatter(with: "yyyy").string(from: Date())
    }

    func year(_ date: Date = Date()) -> String {
        return DateFormatter.formatter(with: "yyyy").string(from: date)
    }

    var month: String {
        return DateFormatter.formatter(with: "MMMM").string(from: Date())
    }

    var simpleMonth: String {
        return DateFormatter.formatter(with: "MMM").string(from: Date())
    }

    var monthNumber: String {
        return DateFormatter.formatter(with: "MM").string(from: Date())
    }

    var week: String {
        return DateFormatter.formatter(with: "EEEE").string(from: Date())
    }

    func simpleWeek(_ date: Date = Date()) -> String {
        return DateFormatter.formatter(with: "EEE").string(from: date)
    }

    var day: String {
        return DateFormatter.formatter(with: "dd").string(from: Date())
    }

    var monthAndDay: String {
        return DateFormatter.formatter(with: Locale.isLanguageZh ? "MM月dd日 " : "MM-dd").string(from: Date())
    }

    func simpleMonthAndDay(_ seperator: String = " ", date: Date = Date()) -> String {
        return DateFormatter.formatter(with: Locale.isLanguageZh ? "M月d日 " : "MMM\(seperator)dd").string(from: date)
    }
}

extension Date {
    var moonIsAM: Bool {
        let hour = Calendar.current.component(.hour, from: self)
        let isAm = hour < 12
        return isAm
    }

    /// AM || PM
    var moonText: String {
        // 阿拉伯特殊用法
        return moonIsAM ? "AM" : "PM"
    }

    func distanceDays(to date: Date = Date()) -> Int {
        let dateStart = Calendar.current.startOfDay(for: date)
        let ts = timeIntervalSince(dateStart)
        return abs(Int(ts) / 24 / 60 / 60)
    }
}
